package com.shuimin.as;

import com.shuimin.as.model.sys.User;
import com.shuimin.as.service.sys.SysRoles;
import pond.common.S;
import pond.db.Record;
import pond.security.rbac.RBAC;
import pond.web.*;
import pond.web.restful.API;

import java.util.List;


/**
 * 系统管理-员工登陆登出等...
 * Created by sky on 15/10/31.
 */
public class SYSTEM extends API {

    {
        get("/login_user",(req,resp)->{
            resp.render(Render.json(((User) Session.get(req).get(App.LOGIN_USER))
                    .refineViewShops()
                    .refineRoles()
                    .refineShop()));
        });

        get("/menu",(req,resp)->{

            RBAC.Roles roles = App.currentRoles();
            List<Record> menus = S._for(SysRoles.menus)
                    .filter(record -> roles.hasEvery((String) record.get("id")))
                    .toList();
            resp.render(Render.json(menus));
        });

        get("/logout",(req,resp)->{
            Session.get(req).invalidate();
            resp.send(204);
        });
    }
}
